package com.yc.backstage.service.serviceImpl;

import cn.hutool.core.convert.Convert;
import com.yc.backstage.common.constant.CacheConstants;
import com.yc.backstage.entity.SysConfig;
import com.yc.backstage.mapper.SysConfigMapper;
import com.yc.backstage.service.SysConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.common.utils.RedisUtils;
import com.yc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Classname SysConfigController
 * @Description
 * @Version 1.0.0
 * @Date 2022-08-23 11:49:19
 * @Created by Mikechen
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 根据键名查询参数配置信息
     *
     * @param configKey 参数key
     * @return 参数键值
     */
    @Override
    public String selectConfigByKey(String configKey) {
        String configValue = Convert.toStr(redisUtils.getCacheObject(getCacheKey(configKey)));
        if (StringUtils.isNotEmpty(configValue)) {
            return configValue;
        }
        SysConfig config = new SysConfig();
        config.setConfigKey(configKey);
        SysConfig retConfig = sysConfigMapper.selectConfig(config);
        if (StringUtils.isNotNull(retConfig)) {
            redisUtils.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
            return retConfig.getConfigValue();
        }
        return StringUtils.EMPTY;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey) {
        return CacheConstants.SYS_CONFIG_KEY + configKey;
    }
}
